<?php

namespace app\api\controller;

use app\common\controller\Base;
use app\common\model\ApprovesFlow;

/**
 * 规则接口
 */
class Rule extends Base
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    protected $pid = 2;


    /**
     * 审批流列表
     */
    public function approves()
    {
        $data = ApprovesFlow::getLists();
        $this->success('success', $data);
    }

    /*
     * 审批人列表
     */
    public function approve_users()
    {
        $data = \app\common\model\User::getApproveUsers();
        $this->success('审批人列表',$data);
    }

    /**
     * 审批添加 / 编辑
     */
    public function approve_edit()
    {
        repeat_filter();        // 防抖
        $params = $this->request->post();
        //验证数据
        $this->apiValidate($params, \app\api\validate\Approve::class,'approve_edit');
        $id = 0;
        extract($params);
        if (!count($user_ids)){
            $this->error('审批人不能为空');
        }
        //如果id不存在执行添加操作
        if (!isset($id) || !$id) {
            $edit = ApprovesFlow::create([
               'type'       => $type,
               'title'      => $title,
               'status'     => 'normal',
               'user_ids'   => json_encode($user_ids,JSON_UNESCAPED_UNICODE),
            ]);
        }else{
            $edit = ApprovesFlow::update([
                'type'       => $type,
                'title'      => $title,
                'user_ids'   => json_encode($user_ids,JSON_UNESCAPED_UNICODE),
                'id'         => $id,
            ]);
        }
        $this->success('操作成功',$edit);
    }

    /**
     * 审批删除
     */
    public function approve_del()
    {
        $id = $this->request->post('id');
        if (!$id){
            $this->error(__('Invalid parameters'));
        }
        $row = ApprovesFlow::get($id);
        if (!$row){
            $this->error('数据不存在或已被删除');
        }
        $row->delete();
        $this->success('操作成功');
    }

}
